package com.foodost.idiomer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;

public class IdiomInitTask extends AsyncTask<Context, Integer, String> {

    private static final String TAG = IdiomInitTask.class.getSimpleName();
    private FinalDb fDB;
    private Context mContext;
    @Override
    protected String doInBackground(Context... params) {
        mContext = params[0];
        fDB = IdiomModel.getFinalDb(mContext);
        Log.v(TAG, "start = " + SystemClock.currentThreadTimeMillis());
        readOriginData();
         Log.v(TAG, "write end = " + SystemClock.currentThreadTimeMillis());
         Utils.idiomInitFilish(mContext);
        return "SUCCESS";
    }
    
    //@TODO待优化，估计占内存比较大
    public void readOriginData(){
        try {
            String[] listPath = mContext.getResources().getAssets().list("idiom");
            
            for(int i = 0; i< listPath.length; i++){
                String path = listPath[i];
                Log.d(TAG, "length = " + listPath.length + " path =" + path);
                InputStream in = mContext.getResources().getAssets().open("idiom/"+path);
                InputStreamReader is = new InputStreamReader(in);
                BufferedReader br = new BufferedReader(is);
                StringBuilder sb = new StringBuilder();
                String strLine = null;
                while((strLine = br.readLine()) != null){
                    sb.append(strLine);
                }
                packageDao(sb.toString(),path);
                br.close();
                is.close();
                in.close();
                if(i == 0){
                    Intent intent = new Intent();
                    intent.setClass(mContext, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    private void packageDao(String coredata,String fileName){
        String [] idiomsArr = coredata.split(";");
        for(String idioms : idiomsArr){
            IdiomData idiom = new IdiomData();
            if(idioms.trim().length() >0){
                String [] idiomArr = idioms.split(",");
                if(idiomArr.length > 1){
                    idiom.setIdiomRight(idiomArr[0].trim());
                    idiom.setIdiomWrong1(idiomArr[1].trim());
                    idiom.setIdiomWrong2(null);
                    idiom.setIdiomWrong3(null);
                    idiom.setIdiomPinYin(null);
                    if(fileName.startsWith("idiom1")){
                        idiom.setIdiomGrage(1);
                    }else if(fileName.startsWith("idiom2")){
                        idiom.setIdiomGrage(2);
                    }else if(fileName.startsWith("idiom3")){
                        idiom.setIdiomGrage(3);
                    }
                    idiom.setIdiomExplan(null);
                    fDB.save(idiom);
                }
                
            }
            
        }
    }

}
